#include "Factory.h"
#include <algorithm>

// static variables need to be explicitly added to one CPP file
vector<Object*> Factory::m_objectList;


void Factory::DeleteObject(Object *o)
{
	for (int i = 0; i < m_objectList.size(); ++i)
	{
		if (m_objectList[i] == o)
		{
			// We've found it!  Remove from list and delete:
			m_objectList[i] = NULL;
			delete o;
			return;
		}
	}
}

void Factory::AddObject(Object *o)
{
	m_objectList.push_back(o);
}

void Factory::RenderAll(sf::RenderWindow &win)
{
	for (int i = m_objectList.size() - 1; i >= 0; --i)
	{
		m_objectList[i]->Render(win);
	}
}

void Factory::UpdateAll(float deltaTime)
{
	for (int i = 0; i < m_objectList.size(); ++i)
	{
		m_objectList[i]->Update(deltaTime);
	}

	// Clean up empty items from our list:
	m_objectList.erase(
		std::remove(m_objectList.begin(), m_objectList.end(), (Object*)NULL),
		m_objectList.end()
		);
}

void Factory::AlertObjects(sf::Event &e)
{
	for (int i = 0; i < m_objectList.size(); ++i)
	{
		bool done = m_objectList[i]->Alert(e);
		if (done) return;
	}
}